/**
 * Created by zhaojm on 15/3/18.
 */
game.UILayer = cc.Layer.extend({
    numScore : null,
    numHeart : null,

    //heartList : null,



    ctor:function(){
        this._super();
        //cc.log("uilayer....ctor");
        this.initHeart(game.Config.fishRes.heart);



        var coin = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('coin.png'));
        this.addChild(coin);
        coin.setAnchorPoint(cc.p(1, 0.5));
        coin.setPosition(cc.p(cc.winSize.width * 0.3, cc.winSize.height * 0.95));
        coin.setScale(0.5);

        this.numScore = cc.LabelTTF.create('00000', 'Arial', 12);
        this.numScore.setPosition(cc.p(coin.getPositionX() + 10, cc.winSize.height * 0.95));
        this.numScore.setAnchorPoint(cc.p(0, 0.5));
        this.addChild(this.numScore);

        var fish = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('sun.png'));
        this.addChild(fish);
        fish.setAnchorPoint(cc.p(1, 0.5));
        fish.setScale(0.24);
        fish.setPosition(cc.p(cc.winSize.width * 0.6, cc.winSize.height * 0.95));

        this.distance = cc.LabelTTF.create('00000', 'Arial', 12);
        this.distance.setPosition(cc.p(fish.getPositionX() + 10, cc.winSize.height * 0.95));
        this.distance.setAnchorPoint(cc.p(0, 0.5));
        this.addChild(this.distance);





        //var logo = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('logo.png'));
        //this.addChild(logo);
        //logo.setPosition(cc.p(cc.winSize.width * 0.85, cc.winSize.height * 0.93));
        ////logo.setScale(0.5);
        var logoframe = cc.spriteFrameCache.getSpriteFrame('logo_2.png');
        var logoItem = new cc.MenuItemImage(logoframe, logoframe, logoframe, this.onMenuCallback, this);

        logoItem.setScale(0.3);

        var menu = new cc.Menu(logoItem);
        //menu.alignItemsVertically();
        //menu.alignItemsHorizontally();
        this.addChild(menu);
        menu.setPosition(cc.p(0, 0));
        logoItem.setPosition(cc.p(cc.winSize.width * 0.85, cc.winSize.height * 0.93));

        that = this;

    },
    onMenuCallback:function(){
        window.location.href="http://mingz.me";
    },


    setDistance:function(num){
        var distance =  num;
        var strDistance = distance + "";
        var l = strDistance.length;

        var needZero = 5 - l;

        var result = '';
        for(var i = 0; i < needZero; i++){
            result += "0";
        }

        result += strDistance;


        this.distance.setString(result);
    },

    setScore:function(num){
        var score = game.Config.timeScore * num;
        var strScore = score + "";
        var l = strScore.length;

        var needZero = 5 - l;

        var result = '';
        for(var i = 0; i < needZero; i++){
            result += "0";
        }

        result += strScore;


        this.numScore.setString(result);
    },

    initHeart:function(num){
        //cc.log('initHeart....num=', num);
        this.numHeart = num;
        //var startLeft = cc.winSize.width * 0.05;
        //var defaultHeight = cc.winSize.height * 0.95;
        //this.heartList = [];
        //for(var i = 0; i < num; i++){
        //    cc.log('i=', i);
        //    var heart = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('heart.png'));
        //    if(this.heartList.length == 0){
        //        heart.setPosition(startLeft, defaultHeight);
        //    }else{
        //        heart.setPosition(this.heartList[this.heartList.length - 1].getPositionX() + heart.getContentSize().width, defaultHeight);
        //    }
        //    this.heartList.push(heart);
        //    this.addChild(heart);
        //}
        var heart = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('heart.png'));
        heart.setAnchorPoint(cc.p(1, 0.5));
        var startLeft = cc.winSize.width * 0.1;
        var defaultHeight = cc.winSize.height * 0.95;
        heart.setPosition(startLeft, defaultHeight);
        this.addChild(heart);
        heart.setScale(0.5);

        this.numHeart = cc.LabelTTF.create('X' + num, 'Arial', 12);
        this.numHeart.setPosition(cc.p(heart.getPositionX() + 10, cc.winSize.height * 0.95));
        this.numHeart.setAnchorPoint(cc.p(0, 0.5));
        this.addChild(this.numHeart);

    },

    setHeart:function(num){
        //if(this.heartList.length > 0){
        //    this.removeChild(this.heartList[this.heartList.length - 1]);
        //    this.heartList.splice(-1, 1);   // 从最后一个位置，删除 一个元素
        //}

        this.numHeart.setString('X' + num);

    },

    startGuide:function(){
        //this.isCanMove = false;
        //this.hero.isCanMove = false;
        this.guideTask =0;
        //TODO 手 点击可以跳一次
        this._colorLayer = new game.GuideLayer();
        this.addChild(this._colorLayer);

        this._bg = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('guide/bg.png'));
        this._bg.setPosition(cc.winSize.width * 0.5, cc.winSize.height * 0.5);
        this.addChild(this._bg);



        //this._light = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('guide/light.png'));
        ////this._light.setScale(cc.winSize.width / this._guideSprite.getContentSize().width);
        //this._light.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height * 0.5));
        ////this._light.setBlendFunc(gl.SRC_ALPHA, gl.ONE);
        ////this._guideSprite.setAnchorPoint(cc.p(0, 0));
        //this.addChild(this._light);

        //this._hand = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('guide/hand.png'));
        //this.addChild(this._hand);
        //this._hand.setAnchorPoint(cc.p(0, 1));
        //this._hand.setPosition(cc.winSize.width / 2, cc.winSize.height * 0.4);

        this._guideTxt = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame('guide/text0.png'));
        this.addChild(this._guideTxt);
        this._guideTxt.setPosition(cc.winSize.width * 0.5, cc.winSize.height * 0.7);

        var buttonFrame = cc.spriteFrameCache.getSpriteFrame('guide/next.png');
        this._guideButton = new cc.MenuItemImage(buttonFrame,buttonFrame,buttonFrame, this.onGuideButtonCallback, this);
        //this._guideButton.setAnchorPoint(cc.p(1, 1));
        //this.addChild(this._guideButton, 5);
        this._guideButton.setPosition(cc.p(cc.winSize.width / 2, cc.winSize.height * 0.4));
        this._menu = new cc.Menu(this._guideButton);
        this._menu.setPosition(cc.p(0, 0));
        this.addChild(this._menu, 5);

        //this.guideLayer.setSpriteVisible(true);
        //this.guideLayer.setSpritePosition(cc.p(cc.winSize.width / 2 - this.getPositionX(), cc.winSize.height / 2));
        //this.guideLayer.setEnabled(true);
    },

    onGuideButtonCallback:function(sender){

        //cc.log('button call back');
        var self = sender.parent.parent;

        if(self.guideTask == 0){
            self.beginGuide2();
        }else if(self.guideTask == 1){
            self.beginGuide3();
        } else if(self.guideTask == 2){
                self.beginGuide4();
        }else if(self.guideTask == 3){
            self.onGuideTask2();
        }
    },


    beginGuide2 : function(){
        //this.isCanMove = false;
        //this.hero.isCanMove = false;
        this.guideTask = 1;
        // TODO 手 点击可以跳得更高
        //this._guideSprite.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('guide0.png'));
        //this._guideSprite.setPositionX(cc.winSize.width / 2 - this.getPositionX());
        //this._guideSprite.setVisible(true);
        //this._guideButton.setVisible(true);
        //this._guideButton.setPosition(cc.p(cc.winSize.width - 10 - this.getPositionX(), cc.winSize.height - 10));
        this._guideTxt.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('guide/text1.png'));

        //this.touchLayer.setTouchEnabled(false);
        //this.guideLayer.setSpriteVisible(true);
        //this.guideLayer.setSpritePosition(cc.p(cc.winSize.width / 2 - this.getPositionX(), cc.winSize.height / 2));
        //this.guideLayer.setEnabled(true);
    },
    beginGuide3:function(){
        //this.isCanMove = false;
        //this.hero.isCanMove = false;
        this.guideTask = 2;
        // TODO 点击开始游戏吧
        //this._guideSprite.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('guide2.png'));
        //this._guideSprite.setPositionX(cc.winSize.width / 2 - this.getPositionX());
        //this._guideSprite.setVisible(true);
        //this._guideButton.setVisible(true);
        //this._guideButton.setPosition(cc.p(cc.winSize.width - 10 - this.getPositionX(), cc.winSize.height - 10));
        //this.touchLayer.setTouchEnabled(false);
        //this.guideLayer.setSpriteVisible(true);
        //this.guideLayer.setSpritePosition(cc.p(cc.winSize.width / 2 - this.getPositionX(), cc.winSize.height / 2));
        //this.guideLayer.setEnabled(true);
        //var okframe = cc.spriteFrameCache.getSpriteFrame('guide/ok.png');
        //this._guideButton.setDisabledSpriteFrame(okframe);
        //this._guideButton.setSelectedSpriteFrame(okframe);
        //this._guideButton.setNormalSpriteFrame(okframe);
        this._guideTxt.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('guide/text2.png'));
    },
    beginGuide4:function(){
        //this.isCanMove = false;
        //this.hero.isCanMove = false;
        this.guideTask = 3;
        // TODO 点击开始游戏吧
        //this._guideSprite.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('guide2.png'));
        //this._guideSprite.setPositionX(cc.winSize.width / 2 - this.getPositionX());
        //this._guideSprite.setVisible(true);
        //this._guideButton.setVisible(true);
        //this._guideButton.setPosition(cc.p(cc.winSize.width - 10 - this.getPositionX(), cc.winSize.height - 10));
        //this.touchLayer.setTouchEnabled(false);
        //this.guideLayer.setSpriteVisible(true);
        //this.guideLayer.setSpritePosition(cc.p(cc.winSize.width / 2 - this.getPositionX(), cc.winSize.height / 2));
        //this.guideLayer.setEnabled(true);
        var okframe = cc.spriteFrameCache.getSpriteFrame('guide/ok.png');
        this._guideButton.setDisabledSpriteFrame(okframe);
        this._guideButton.setSelectedSpriteFrame(okframe);
        this._guideButton.setNormalSpriteFrame(okframe);
        this._guideTxt.setSpriteFrame(cc.spriteFrameCache.getSpriteFrame('guide/text3.png'));
    },


    //onGuideTask0 : function(){
    //    this.isCanMove = true;
    //    this.hero.isCanMove = true;
    //    this.hero.isGuideTask = 0;
    //    this._guideSprite.setVisible(false);
    //    this._guideButton.setVisible(false);
    //
    //    //this.guideLayer.setSpriteVisible(false);
    //    //this.guideLayer.setEnabled(false);
    //},
    //
    //onGuideTask1:function(){
    //    this.isCanMove = true;
    //    this.hero.isCanMove = true;
    //    this.hero.isGuideTask = 1;
    //    this._guideSprite.setVisible(false);
    //    this._guideButton.setVisible(false);
    //    //this.guideLayer.setSpriteVisible(false);
    //    //this.guideLayer.setEnabled(false);
    //},
    onGuideTask2:function(){
        this.parent.mainLayer.isCanMove = true;
        this.parent.mainLayer.hero.isCanMove = true;
        //this.hero.isGuideTask = -1;
        //this.touchLayer.guideTask = -1;
        //this._hand.removeFromParent();
        this._bg.removeFromParent();
        //this._light.removeFromParent();
        this._colorLayer.removeFromParent();
        this._guideButton.removeFromParent();
        this._guideTxt.removeFromParent();
        this.parent.mainLayer.touchLayer.setTouchEnabled(true);
        cc.sys.localStorage.setItem('guide', '1');
    },


});